<template>
  <el-dialog
    :title="!dataForm.id ? 'Create' : 'Modify'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item> -->
    <el-form-item label="Name" prop="username">
      <el-input v-model="dataForm.username" placeholder="Name"></el-input>
    </el-form-item>
    <el-form-item label="Email" prop="email">
      <el-input v-model="dataForm.email" placeholder="Email"></el-input>
    </el-form-item>
    <!-- <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="手机验证" prop="mobileCheck">
      <el-input v-model="dataForm.mobileCheck" placeholder="手机验证"></el-input>
    </el-form-item> -->
    <el-form-item label="Password" prop="password">
      <el-input v-model="dataForm.password" placeholder="Password"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          createTime: '',
          username: '',
          email: '',
          mobile: '',
          mobileCheck: '',
          password: ''
        },
        dataRule: {
          username: [
            { required: true, message: 'Name can not null', trigger: 'blur' }
          ],
          email: [
            { required: true, message: 'Email can not null', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/memberhpr/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.user.createTime
                this.dataForm.username = data.user.username
                this.dataForm.email = data.user.email
                this.dataForm.mobile = data.user.mobile
                this.dataForm.mobileCheck = data.user.mobileCheck
                this.dataForm.password = data.user.password
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        var _this = this
        _this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            _this.$http({
              url: _this.$http.adornUrl(`/memberhpr/user/${!_this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: _this.$http.adornData({
                'userId': _this.dataForm.userId || undefined,
                'createTime': _this.dataForm.createTime,
                'username': _this.dataForm.username,
                'email': _this.dataForm.email,
                'mobile': _this.dataForm.mobile,
                'mobileCheck': _this.dataForm.mobileCheck,
                'password': _this.dataForm.password
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                _this.$message.success('success', 1, function () {
                  _this.visible = false
                  _this.$emit('refreshDataList')
                })
              } else {
                _this.$message.error(data.msg, 1)
              }
            })
          }
        })
      }
    }
  }
</script>
